let dayjs = null

async function initDayjs() {
  if (!dayjs) {
    const dayjsModule = await import('dayjs')
    dayjs = dayjsModule.default
    await import('dayjs/locale/zh-cn')
    dayjs.locale('zh-cn')
  }
  return dayjs
}

export const formatDate = async (date, format = 'YYYY-MM-DD HH:mm:ss') => {
  if (!date) return ''
  const d = await initDayjs()
  return d(date).format(format)
}

export const formatRelativeTime = async (date) => {
  if (!date) return ''
  const d = await initDayjs()
  return d(date).fromNow()
}

export const formatDuration = (seconds) => {
  if (!seconds) return '0秒'
  const hours = Math.floor(seconds / 3600)
  const minutes = Math.floor((seconds % 3600) / 60)
  const remainingSeconds = seconds % 60
  
  let result = ''
  if (hours > 0) result += `${hours}小时`
  if (minutes > 0) result += `${minutes}分钟`
  if (remainingSeconds > 0 || result === '') result += `${remainingSeconds}秒`
  
  return result
} 